package graduate.cslg.com.universityshopping.mainPage;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import graduate.cslg.com.universityshopping.card.CardActivity;
import graduate.cslg.com.universityshopping.card.DailyAccountActivity;
import graduate.cslg.com.universityshopping.webView.EducationalActivity;
import graduate.cslg.com.universityshopping.webView.SchoolNewsContentActivity;
import graduate.cslg.com.universityshopping.bbs.BBSActivity;
import graduate.cslg.com.universityshopping.MainActivity;
import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.ZhiHubean.LatestNews;
import graduate.cslg.com.universityshopping.adapter.NewsSummaryAdapter;
import graduate.cslg.com.universityshopping.base.BaseFragment;
import graduate.cslg.com.universityshopping.dormitory.DormitoryActivity;
import graduate.cslg.com.universityshopping.entity.CardBean;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static android.content.Context.MODE_PRIVATE;

public class MainFragment extends BaseFragment implements View.OnClickListener {

    private TextView userBaseInfo;
    private ImageView codeImage;
    private ImageView showImage;
    private TextView userBalance;
    private Button chargeButton;
    private RecyclerView weekNews;
    private LinearLayout mLayout1;
    private LinearLayout mLayout2;
    private LinearLayout mLayout3;
    private LinearLayout mLayout4;
    private LinearLayout mLayout5;
    private LinearLayout mLayout6;
    private LinearLayout mLayout7;
    private LinearLayout mLayout8;
    private ImageView img_tog;
    private NewsSummaryAdapter mNewsSummaryAdapter;
    private LatestNews mLatestNews;
    private List<LatestNews.StoriesBean> mStoriesBeanList;
    StringBuffer baseInfo;
    private String userAccount;
    private CardBean mCardBean;

    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.main_fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(getLayoutResId(), container, false);
        initView(rootView);
        return rootView;
    }

    private void initView(View view) {
        img_tog = view.findViewById(R.id.img_tog);
        img_tog.setOnClickListener(this);
        userBaseInfo = view.findViewById(R.id.user_base_info);
        userBaseInfo.setOnClickListener(this);
        codeImage = view.findViewById(R.id.get_scan_img);
        codeImage.setOnClickListener(this);
        userBalance = view.findViewById(R.id.account_balance);
        userBalance.setOnClickListener(this);
        showImage = view.findViewById(R.id.main_fragment_show_image);
        showImage.setOnClickListener(this);
        chargeButton = view.findViewById(R.id.re_charge);
        chargeButton.setOnClickListener(this);
        weekNews = view.findViewById(R.id.main_week_top_news);
        mLayout1 = view.findViewById(R.id.ll_1);
        mLayout2 = view.findViewById(R.id.ll_2);
        mLayout3 = view.findViewById(R.id.ll_3);
        mLayout4 = view.findViewById(R.id.ll_4);
        mLayout5 = view.findViewById(R.id.ll_5);
        mLayout6 = view.findViewById(R.id.ll_6);
        mLayout7 = view.findViewById(R.id.ll_7);
        mLayout8 = view.findViewById(R.id.ll_8);
        mLayout1.setOnClickListener(this);
        mLayout2.setOnClickListener(this);
        mLayout3.setOnClickListener(this);
        mLayout4.setOnClickListener(this);
        mLayout5.setOnClickListener(this);
        mLayout6.setOnClickListener(this);
        mLayout7.setOnClickListener(this);
        mLayout8.setOnClickListener(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        weekNews.setLayoutManager(layoutManager);
        getNewsInfo();
        SharedPreferences preferences = getActivity().getSharedPreferences("user_info",MODE_PRIVATE);
        baseInfo = new StringBuffer();
        userAccount = preferences.getString("user_account", "Z09315222");
        baseInfo.append(userAccount).append(" ").append(preferences.getString("user_name", "Tom Wang"));
        userBaseInfo.setText(baseInfo.toString());
        getBalance(userAccount);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.get_scan_img:

                break;

            case R.id.re_charge:
                Toast.makeText(getActivity(), "充值功能尚在测试中", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_1:
                startActivity(new Intent(getActivity(), PunchActivity.class));
                break;
            case R.id.ll_2:
                startActivity(new Intent(getActivity(), SchoolNewsContentActivity.class));
                break;
            case R.id.ll_3:
                startActivity(new Intent(getActivity(), BBSActivity.class));
                break;
            case R.id.ll_4:
                startActivity(new Intent(getActivity(), CardActivity.class));
                break;
            case R.id.ll_5:
                startActivity(new Intent(getActivity(), DailyAccountActivity.class));
                break;
            case R.id.ll_6:
                startActivity(new Intent(getActivity(), DormitoryActivity.class));
                break;
            case R.id.ll_7:
                startActivity(new Intent(getActivity(), GetLostActivity.class));
                break;
            case R.id.ll_8:
                startActivity(new Intent(getActivity(), EducationalActivity.class));
                break;
            case R.id.img_tog:
                toggleDrawer();
                break;
        }
    }

    /**
     * 打开侧滑功能
     * */
    void toggleDrawer() {
        Activity activity = getActivity();
        if (activity instanceof MainActivity) {
            ((MainActivity) activity).toggleDrawer();
        }
    }

    private void getNewsInfo() {
        RetrofitDevelop.getZhiHuService()
                .getLatestNews()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LatestNews>() {
                    @Override
                    public void onCompleted() {
                        mStoriesBeanList = mLatestNews.getStories();
                        mNewsSummaryAdapter = new NewsSummaryAdapter(mStoriesBeanList);
                        weekNews.setAdapter(mNewsSummaryAdapter);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(LatestNews latestNews) {
                        mLatestNews = latestNews;
                    }
                });
    }

    private void getBalance(String userAccount) {
        RetrofitDevelop.getTestService()
                .getCardInfo(userAccount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CardBean>() {
                    @Override
                    public void onCompleted() {
                        userBalance.setText(mCardBean.getUser_balance());
                        SharedPreferences preferences = getActivity().getSharedPreferences("user_info",MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("card_balance", mCardBean.getUser_balance());
                        editor.putInt("card_type", mCardBean.getCard_type());
                        editor.apply();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(CardBean cardBean) {
                        mCardBean = cardBean;
                    }
                });
    }
}
